#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>  //添加的头文件
#include <QDebug>       //添加的头文件
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(ui->Choose,SIGNAL(triggered()),this,SLOT(on_Choose_clicked()));
    connect(ui->Gaosi,SIGNAL(triggered()),this,SLOT(on_Gaosi_clicked()));
}


void MainWindow::on_Choose_clicked()
{
    QString OpenFile, OpenFilePath;
        QImage image;
        //打开文件夹中的图片文件
        OpenFile = QFileDialog::getOpenFileName(this,
                                                  "Please choose an image file",
                                                  "",
                                                  "Image Files(*.jpg *.png *.bmp *.pgm *.pbm);;All(*.*)");
        if( OpenFile != "" )
        {
            if( image.load(OpenFile) )
            {
                ui->label->setPixmap(QPixmap::fromImage(image));
            }
        }

}
QImage* MainWindow::on_Gaosi_clicked(){
    QImage*image;
    QImage* newImage = new QImage(image->width(),image->height(),QImage::Format_ARGB32);
        int kernel [3][3] = {
        {1,2,1},
        {2,4,2},
        {1,2,1}};
        int sizeKernel = 3;
        int sumKernel = 16;
        QColor color;
         for(int x = sizeKernel/2;x<image->width() - sizeKernel/2;x++)
        {
           for(int y= sizeKernel/2;y<image->height() - sizeKernel/2;y++)
            {
                int r = 0;
                int g = 0;
                int b = 0;
                for(int i = -sizeKernel/2;i<=sizeKernel/2;i++)
                {
                   for(int j = -sizeKernel/2;j<=sizeKernel/2;j++)
                    {
                     color = QColor(image->pixel(x+i,y+j));
                     r+=color.red()*kernel[sizeKernel/2+i][sizeKernel/2+j];
                     g+=color.green()*kernel[sizeKernel/2+i][sizeKernel/2+j];
                     b+=color.blue()*kernel[sizeKernel/2+i][sizeKernel/2+j];
                    }
                }
                r = qBound(0,r/sumKernel,255);
                g = qBound(0,g/sumKernel,255);
                b = qBound(0,b/sumKernel,255);
                newImage->setPixel(x,y,qRgb( r,g,b));
            }
        }
        return newImage;

}
MainWindow::~MainWindow()
{
    delete ui;
}
